package com.kaili.finance.controller.web.v1;

import com.kaili.common.util.DateUtil;
import com.kaili.finance.pojo.dto.detailSum.finInOutQueryDTO;
import com.kaili.finance.pojo.dto.transSum.FinTransSumQueryDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;

public class BaseController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;

    public Object getSessionByName(String name) {
        return request.getSession().getAttribute(name);
    }

    public void setSession(String name, Object obj) {
        request.getSession().setAttribute(name, obj);
    }

    public HttpSession getSession() {
        return request.getSession();
    }

    public void checkCreateTime(FinTransSumQueryDTO finace,String createTime) throws ParseException {
        //获取前一天日期 23：59:59
        Date yesDate = DateUtil.getYesterDayLastTime(new Date());

        //如果结束时间不为空 为空直接置为前一天 23：59：59
        if(StringUtils.isNoneBlank(finace.getCreatedEndTime())){
            Date endTime = DateUtil.getStrToDateTime(finace.getCreatedEndTime());

            if(endTime.getTime()>yesDate.getTime()){
                finace.setCreatedEndTime(DateUtil.getDateToStr(yesDate));
            }
        }else{
            finace.setCreatedEndTime(DateUtil.getDateToStr(yesDate));
        }
    }

    public void checkCreateTime2(finInOutQueryDTO finace,String createTime) throws ParseException {
        //获取前一天日期 23：59:59
        Date yesDate = DateUtil.getYesterDayLastTime(new Date());

        //如果结束时间不为空 为空直接置为前一天 23：59：59
        if(StringUtils.isNoneBlank(finace.getCreatedEndTime())){
            Date endTime = DateUtil.getStrToDateTime(finace.getCreatedEndTime());

            if(endTime.getTime()>yesDate.getTime()){
                finace.setCreatedEndTime(DateUtil.getDateToStr(yesDate));
            }
        }else{
            finace.setCreatedEndTime(DateUtil.getDateToStr(yesDate));
        }
    }
}
